﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using Model;

namespace BLL
{
    public class TimeCard_BLL
    {
        ExtendedDBModelContainer _db = new ExtendedDBModelContainer();

        public IQueryable<TimeCard> Get()
        {
            //筛选与当前用户同一公司的记录
            var model = _db.TimeCards.Where(a => a.Sys_Users.Sys_Enterprise.Sys_Users.Any(b => b.Id.Equals(new Guid(HttpContext.Current.User.Identity.Name))));

            model = model.OrderByDescending(a => a.AddDate);

            return model;
        }

        public TimeCard Get(Guid? Id)
        {
            var item = new TimeCard();

            if (Id.HasValue)
            {
                item = _db.TimeCards.Single(a => a.Id.Equals(Id.Value));
            }
            return item;
        }

        public void Save(Guid? Id, ref TimeCard item)
        {
            if (!Id.HasValue)
            {
                _db.TimeCards.AddObject(item);
            }
            else
            {
                _db.TimeCards.Attach(item);
                _db.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
            }
            _db.SaveChanges();
        }

        public void Save()
        {
            _db.SaveChanges();
        }

        public void Delete(Guid Id)
        {
            _db.TimeCards.DeleteObject(_db.TimeCards.Single(a => a.Id == Id));
            _db.SaveChanges();
        }
    }
}
